<template>  
 <div class="loginbody">
   <el-header>
        <ye-tou></ye-tou>
      </el-header>     
    <div class="login">
      <div class="mylogin" align="center">    
        <h4 style="color:black">欢迎登陆香格里拉大酒店</h4>      
        <el-form
          :model="loginForm"    
          status-icon     
          ref="loginForm"
          label-width="0px"         
          :rules="rules"
        >
        
          <el-form-item
            label=""
            prop="userName"
            style="margin-top:10px;"
          >
            <!-- <el-row>
              <el-col :span='2'>
                <span class="el-icon-s-custom"></span>
              </el-col> -->
              <!-- <el-col :span='22'> -->
                <el-input
                  class="inps"
                  placeholder='请输入用户名'
                  v-model="loginForm.userName"
                  autocomplete="off"
                  >
                  
                </el-input>
              <!-- </el-col>
            </el-row> -->
          </el-form-item>
          <el-form-item
            label=""
            prop="upwd"
          >
            <!-- <el-row> -->
              <!-- <el-col :span='2'>
                <span class="el-icon-lock"></span>
              </el-col>
              <el-col :span='22'> -->
                <el-input
                  class="inps"
                  type="password"
                  placeholder='清输入密码'
                   autocomplete="off"
                  v-model="loginForm.upwd"
                  ></el-input>
              <!-- </el-col>
            </el-row> -->
          </el-form-item>
   
         <el-checkbox  class="remember" v-model="loginForm.remember">记住密码</el-checkbox>
    
         
          <el-form-item style="margin-top:40px;">
            <el-button
              type="primary"
              round
              class="submitBtn"              
              size="large" @click.native="checkForm('loginForm')"           
              
            >登录  
            </el-button>
          </el-form-item>      
        </el-form>
            <!-- <div class="unlogin">           -->
            <router-link :to="{path: '/register'}">
             注册新账号
            </router-link>
            <router-link :to="{path: '/text'}">
             新大陆
            </router-link>
          <!-- </div> -->
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
 import {mapMutations} from "vuex";
 import YeTou from "@/components/YeTou.vue";
import qs from 'qs'
import {  setCookie, clearCookie, getCookie} from '../api/cookie.js'
export default {
  components: {
    YeTou,
  },
    name: "Login",
    data() {      
    
      return {
         // 记住密码
            
        loginForm: {
          userName: '',
          upwd: '',
          remember: false,  
        },
        rules: {
           userName: [
            { required: true, message: '用户名为必填项', trigger: 'blur' }
            
          ],
          upwd: [ { required: true, message: '密码为必填项', trigger: 'blur' }]
 
        }
      }
    },
    mounted(){
       let {username,upwd}= getCookie()
       this.loginForm.userName=username;
       this.loginForm.upwd=upwd;
    },
     methods: {
    // 验证表单  验证3个Field组件是否合法
    checkForm(formName){  
      this.$refs[formName].validate((valid) => {
        console.log(`valid`,valid);
        if (valid) {
      // if (this.checkName()&&this.checkPwd()) {
         let obj =qs.stringify(this.loginForm)
          console.log(`格式验证验证成功`,this.loginForm);
          this.$axios.post("/users/login",obj).then(result=>{           
            console.log('发请求，执行登录业务...',result.data);             
            // 判断并且提示是否登录成功
            if (result.data.code){ // 登录成功
             console.log(`vip`,result.data.data[0].is_vip);
             let flag=result.data.data[0].is_vip;
              // 弹窗提示登录成功
              this.$message({
                message: '登录成功',
                type:'success'
              })             
              //  this.$store.commit('loginok', result.data.data[0].userName);
               if (this.loginForm.remember) {
                  console.log("checked == true");
                  //传入账号名，密码，和保存天数3个参数
                  setCookie(result.data.data[0].userName, result.data.data[0].upwd, 7);            
                  localStorage.setItem('islogin',true);
                  localStorage.setItem('username',result.data.data[0].userName);
               }else{
                  console.log("清空Cookie");
                  //清空Cookie
                  clearCookie();
                 //将islogin 与username存入sessionStorage
                  sessionStorage.setItem('islogin',true);
                  sessionStorage.setItem('username',result.data.data[0].userName);
               }                  
              if (flag) {
                 // 直接跳转到首页
                 this.$router.replace('/backstage');
              } 
              else {                
                   let redirectUrl = decodeURIComponent(this.$route.query.redirect || '/')
                  console.log(redirectUrl)
                  // 跳转到指定的路由
                  this.$router.push({
                    path: redirectUrl
                  }) 
              }
            }else{  // 登录失败
              console.log(`result.data.code,`,result.data.msg);
              this.$message.error('账号密码输入错误，请重试')
                        //     this.$message.error(res.msg)
          //     this.logining = false
          //     return false
            }
        })
           }else{
             this.$message.error('格式错误')
           }
      })
    },
   
  }
}

</script>

<style scoped>

.remember {
  float: left;
  padding: 10px 5px;
  /* margin: 0px 0px 0px 0px; */
}
.unlogin{
  width: 240px;
  /* height: 100px; */
}
 .loginbody {
    overflow: scroll;
    overflow-y: hidden;
    overflow-x: hidden;
    border-radius: 5px;
  }
 
  .login {    
    width: 100vw;
    padding: 0;
    margin: 0;
    height: 100vh;
    font-size: 16px;
    background-position: left top;
    background-color: #242645;
    color: #fff;
    font-family: "Source Sans Pro";
    position: relative;
    background-image: url('/imags/6.jpg');
    background-repeat: no-repeat;
    background-size: 100% 100%;
  }
 
  .mylogin {
    width: 240px;
    height: 320px;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    padding: 50px 40px 40px 40px;
    box-shadow: -15px 15px 15px rgba(6, 17, 47, 0.7);
    opacity: 1;
    background:rgb(218, 209, 209)
  }
 
  .inps input {
    border: none;
    color: #fff;
    background-color: transparent;
    font-size: 12px;
  }
 
  .submitBtn {
    background-color: white;
    color: #39f;
    width: 200px;
  }

</style>
